const app = getApp()


Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.path,
    doctor: '',
    commentList: [],
    comment: '', //评论内容
    parentId: 0, //回复的评论id
    autoFocus: false,
    myUserId: app.globalData.userId,
    userType: app.globalData.userType,
    resIndex: -1,
  },

  sendHandle() {
    console.log(this.data.comment);
    console.log(this.data.doctor);

    if (this.data.comment == '') {
      wx.showToast({
        title: '请说点什么',
        icon: 'none'
      })
      return
    }

    var url = app.globalData.url + "main/addDoctorTalk", that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        doctorId: that.data.doctor.doctorId,
        userId: app.globalData.userId,
        talkDetail: that.data.comment,
        parentId: that.data.parentId,
        userType: that.data.userType == 2 ? 1 : 0,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.showToast({
            title: '发布成功',
          })
          that.setData({
            comment: '',
            autoFocus: false,
            parentId: 0,
            resIndex: -1
          })
          wx.hideLoading()
          that.getComment()
        }
      },
      err => {
        console.log(err);
      })
  },


  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
  },

  // 获取评论
  getComment() {
    var url = app.globalData.url + "main/getDoctorTalk",
      that = this,
      commontList = [];
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        doctorId: that.data.doctor.doctorId
      }, res => {
        console.log(res);
        var datas = res.data;
        if (res.flag) {
          wx.hideLoading()

          console.log(datas);


          commontList = datas.filter(item => {
            return item.parentId == 0
          })

          console.log(commontList);
          commontList.forEach(ele => {
            ele.childList = []
            datas.forEach(item => {
              if (ele.talkId == item.parentId) {
                ele.childList.push(item)
              }
            })
            ele.childList.reverse()
          })

          console.log(commontList);
          that.setData({
            commentList: commontList
          })
          console.log(datas);
        }
      },
      err => {
        console.log(err);
      })
  },

  avatarError(e) {
    console.log(e);
    var outIndex = e.currentTarget.dataset.imgindex,
      list = this.data.commentList,
      src = '/img/userImg.jpg';
    list[outIndex].userImg = src
    this.setData({
      commentList: list
    })
  },

  doctorError() {
    var doctor = this.data.doctor
    doctor.doctorPic = '/img/userImg.jpg'
    this.setData({
      doctor
    })
  },
  bgError() {
    var doctor = this.data.doctor
    doctor.doctorPic = '/img/userImg.jpg'
    this.setData({
      doctor
    })
  },
  inputBlur() {
    this.setData({
      resIndex: -1,
      autoFocus: false
    })
  },
  resComHandle(e) {
    var id = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.index;

    console.log(index);
    this.setData({
      parentId: id,
      autoFocus: true,
      resIndex: index
    })
    console.log(this.data);
  },

  toRegistration() {
    var doctor = this.data.doctor
    wx.navigateTo({
      url: '/hospital/pages/registrationDetails/registrationDetails?doctor=' + JSON.stringify(doctor),
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    var doctor = JSON.parse(options.doctor)

    this.setData({
      doctor,
    })

    this.getComment()

    console.log(this.data);
    console.log(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})